package com.changyue.activity.history;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.changyue.activity.basic.BasicActivity;
import com.changyue.activity.history.entity.HistoryDataEntity;
import com.changyue.bleHelper.BLEConstant;
import com.changyue.temp.R;
import com.changyue.temp.databinding.ActivityHistoryBinding;
import com.changyue.util.EmptyUtil;

import java.util.ArrayList;
import java.util.List;

public class HistoryActivity extends BasicActivity {

    public final static String TAG = "HistoryActivity";
    ActivityHistoryBinding binding;

    private List<HistoryDataEntity> mList;
    private HistorytAdapter mAdapter;

    public static void start(Context context) {
        Intent intent = new Intent(context, HistoryActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityHistoryBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        mList = new ArrayList<HistoryDataEntity>();

        mAdapter = new HistorytAdapter(this, mList);
        binding.historyList.setAdapter(mAdapter);

        binding.historyList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                HistoryDataEntity entity = HistoryActivity.this.mList.get(position);
                HistoryInfoActivity.start(HistoryActivity.this, entity.getId());
            }
        });

        binding.historyList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                HistoryDataEntity entity = HistoryActivity.this.mList.get(position);

                AlertDialog.Builder singleChoiceDialog = new AlertDialog.Builder(HistoryActivity.this);
                singleChoiceDialog.setTitle("删除提示");
                singleChoiceDialog.setMessage("是否需要删除，名称:" + entity.save_date + "的历史数据吗？");
                singleChoiceDialog.setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        final boolean ret = DataManager.delete(entity);
                                        HistoryActivity.this.mList.remove(entity);
                                        HistoryActivity.this.runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                mAdapter.notifyDataSetChanged();
                                                Toast.makeText(HistoryActivity.this, ret ? "删除成功" : "删除失败", Toast.LENGTH_LONG).show();
                                            }
                                        });
                                    }
                                }).start();

                            }
                        });
                singleChoiceDialog.setNegativeButton("取消", null);
                singleChoiceDialog.show();

                return true;
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                List<HistoryDataEntity> historyDataEntities = DataManager.getHistory();
                HistoryActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mList.addAll(historyDataEntities);
                        if (EmptyUtil.isNotEmpty(historyDataEntities)) {
                            binding.noHistory.setVisibility(View.GONE);
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                });
            }
        }).start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.histroy_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
            case R.id.delete_all:
            {
                AlertDialog.Builder singleChoiceDialog = new AlertDialog.Builder(HistoryActivity.this);
                singleChoiceDialog.setTitle("删除提示");
                singleChoiceDialog.setMessage("是否需要删除，全部的的历史数据吗？");
                singleChoiceDialog.setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                HistoryActivity.this.mList.clear();
                                mAdapter.notifyDataSetChanged();

                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        DataManager.clearHistory();
                                        HistoryActivity.this.runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(HistoryActivity.this, "删除成功", Toast.LENGTH_LONG).show();
                                            }
                                        });
                                    }
                                }).start();
                            }
                        });
                singleChoiceDialog.setNegativeButton("取消", null);
                singleChoiceDialog.show();

            }
                break;

            default:

                break;
        }
        return true;
    }

    class HistorytAdapter extends BaseAdapter {
        private List<HistoryDataEntity> dataEntities;
        private LayoutInflater mInflater;

        public HistorytAdapter(Context context, List<HistoryDataEntity> devices) {
            mInflater = LayoutInflater.from(context);
            dataEntities = devices;
        }

        public int getCount() {
            return dataEntities.size();
        }

        public Object getItem(int position) {
            return dataEntities.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewGroup vg;

            if (convertView != null) {
                vg = (ViewGroup) convertView;
            } else {
                vg = (ViewGroup) mInflater.inflate(R.layout.history_item, null);
            }

            HistoryDataEntity itemDevice = dataEntities.get(position);
            String workMethod = BLEConstant.get_WORK_METHOD_swv_cmd_name(itemDevice.workMethod);
            ((TextView) vg.findViewById(R.id.title)).setText(itemDevice.save_date + " " + workMethod);

            return vg;
        }
    }
}
